package com.example.mapp.buildSql;

import com.example.ext.entity.ExtSysUser;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/8/13.
 *
 */
public class SysUserMappSql {

    /**
     * 查询用户信息
     * @param userId
     * @return
     */
    public String getSysUser(final Long userId){
        //sur.role_id,sr.role_name,
        String select = " DISTINCT e.author_name,e.parent_id,e.author_url,e.author_icon,e.id as author_id ";

        String table = " sys_user as a left join sys_user_role as b on a.id = b.user_id " +
                " left join system_role as c on b.role_id = c.id " +
                " left join sys_role_author as d on d.role_id = c.id  " +
                " left join system_author as e on d.author_id = e.id  " ;
        String where = " e.sys_type='pc' and e.author_type='menu' and a.id=#{userId}  ";
        return new SQL(){{
            SELECT(select);
            FROM(table);
            WHERE(where);
            GROUP_BY("author_id");
            }
        }.toString();
    }

    /**
     * 查询用户列表信息
     * @param systemUser
     * @return
     */
    public String findSysUserList(final ExtSysUser systemUser){

        String select = " s.user_code as userCode, s.user_name as userName, s.user_state as userState, " +
                "s.phone, s.mobile, s.card_number as cardNumber, " +
                " s.version, s.work_number as workNumber,s.email, s.id, s.type, " +
                " s.card_type as cardType, s.card_type_number as cardTypeNumber, s.file_list as fileList, " +
                " s.face_url as faceUrl, s.role_type_id as roleTypeId, s.firstgt , s.update_time as updateTime, " +
                " r.role_name as roleName,r.id as roleId,p.party_name as partyName," +
                " p.id as partyId, p.party_type as partyType ";

        String table = " sys_user as s " +
                " left join party_user as pu on s.id = pu.user_id" +
                " left join sys_user_role sur on sur.user_id=s.id " +
                " left join system_role r on r.id = sur.role_id " +
                " left join party p on p.id=pu.party_id ";
        String conditions ="  1=1 ";
        if(systemUser.getUserName()!=null && systemUser.getUserName().length()>0){
            systemUser.setUserName('%'+systemUser.getUserName()+'%');
            conditions+=" and s.user_name like #{userName}";
        }
        if(systemUser.getPartyId()!=null){
            conditions+= " and p.id = #{partyId}";
        }
        if(systemUser.getPartyIds()!=null && !"".equals(systemUser.getPartyIds())){
        	String[] tmp = systemUser.getPartyIds().split(",");
        	StringBuilder builder = new StringBuilder(" and p.id in (");
            for(int i = 0,len = tmp.length; i < len;i++){
                String partyId = tmp[i];
                if(i == len - 1){
                    builder.append("'").append(partyId).append("'");
                }else{
                    builder.append("'").append(partyId).append("',");
                }
            }
        	conditions += builder.append(") ").toString();
        }
        if(systemUser.getType()!=null && systemUser.getType().length()>0){
            conditions+= " and s.type = #{type}";
        }
        if(systemUser.getPartyType()!=null && systemUser.getPartyType().length()>0){
        	String[] partyTypes = systemUser.getPartyType().split(",");
            conditions+= " and (p.party_type = '" + partyTypes[0] + "' or p.party_type = '" + partyTypes[1] + "') ";
        }
        if(systemUser.getCardType()!=null && systemUser.getCardType().length()>0){
            if(systemUser.getCardType().contains(",")){
                String roleTypeId[] = systemUser.getCardType().split(",");
                conditions+= " and ( s.card_type='"+roleTypeId[0] +"' or s.card_type='"+roleTypeId[1]+"' ) ";
            }else{
                conditions+= " and s.card_type='"+ systemUser.getCardType()+"'";
            }
        }
        conditions += " and not exists (select 1 from user_metastasis um where um.user_id=s.id and um.state='1')";
        String finalConditions = conditions;
        return new SQL(){{
            SELECT(select);
            FROM(table);
            WHERE(finalConditions);
        }
        }.toString();
    }

    /**
     * @deprecated  查询全部用户的功能
     * @param userId
     * @return
     */
    public String findUserFunction(String userId){
        //sur.role_id,sr.role_name,
        String select = " DISTINCT sa.author_name,sa.parent_id,sa.author_url,sa.author_icon,sa.id as author_id";

        String table = " system_author sa  LEFT JOIN sys_role_author as a on a.author_id = sa.id " +
                " LEFT join sys_user_role as b on a.role_id = b.id " ;
        String where = "  b.user_id=#{userId} ";
        return new SQL(){{
            SELECT(select);
            FROM(table);
           // WHERE(where);
            GROUP_BY("author_id");
        }
        }.toString();
    }


    /**
     * @deprecated  查询角色拥有的功能
     * @param roleId
     * @return
     */
    public String findUserFunctionByRoleId(String roleId){
        String select = " DISTINCT c.author_name,c.parent_id,c.author_url,c.author_icon,c.id as author_id";

        String table = "  system_role as a " +
                " INNER JOIN sys_role_author as b on a.id = b.role_id " +
                " INNER join system_author as c on b.author_id = c.id";
        String where = "  a.id = #{roleId} ";
        return new SQL(){{
            SELECT(select);
            FROM(table);
            WHERE(where);
            GROUP_BY("author_id");
        }
        }.toString();
    }

    /**
     *
     * 双随机一公开根据单位Id查询用户信息
     * findList
     * @param
     * @return
     */
    public String queryUserByUnit(Map map){
        StringBuffer sb = new StringBuffer();

        sb.append(" select er.id  as 'id', ");
        sb.append("  work_number  as 'workNumber', ");
        sb.append("  user_name as 'userName', ");
        sb.append("  user_code as 'userCode', ");
        sb.append("  card_type as 'cardType', ");
        sb.append("  card_number as 'cardNumber', ");
        sb.append("  card_type_number as 'typeNumber' ");
        sb.append("  from sys_user er ");
        sb.append("  left join party_user pa ON er.id = pa.user_id ");
        sb.append("  where 1=1 ");

        if(map.get("unitParam") !=null ){
            sb.append(" and pa.party_id in ("+map.get("unitParam").toString()+") ");
        }

        if (map.get("userType") != null) {
            List<String> types = (List<String>) map.get("userType");
            StringBuilder builder = new StringBuilder(" and card_type in ( ");
            for (int i = 0; i < types.size(); i++) {
                String name = types.get(i);
                if (i == types.size() - 1) {
                    builder.append("'").append(name).append("'");
                } else {
                    builder.append("'").append(name).append("',");
                }
            }
            builder.append(")");
            sb.append(builder.append(builder.toString()));
        }
        return sb.toString();
    }
}
